package one.bugu.android.demon.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.SearchBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.activity.SetWarnActivity;
import one.bugu.android.demon.ui.dialog.DialogHint;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.ParamUtils;
import one.bugu.android.demon.util.PreferencesUtil;

/**
 * 搜索结果列表适配
 */
public class MyExtendableListViewAdapter extends BaseExpandableListAdapter implements DialogHint.OnDialogVersionListener {

    private Context context;

    /**
     * 弹框
     */
    private DialogHint dialogHint;

    private List<SearchBean> list;//定义父级列表

    String token;

    public MyExtendableListViewAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();

        dialogHint = new DialogHint(context,"自选成功！",true);
        dialogHint.setCancleText("继续自选");
        dialogHint.setOkText("前往设置");
        dialogHint.setOnDialogVersionListener(this);
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return list.get(i).getList().size();
    }

    @Override
    public Object getGroup(int i) {
        return getGroup(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return list.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.partent_item, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.se_pt = convertView.findViewById(R.id.se_pt);
            groupViewHolder.se_ptIcon = convertView.findViewById(R.id.se_ptIcon);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        //设置平台名字
        groupViewHolder.se_pt.setText(list.get(groupPosition).getPlatName());

        //根据平台名字 设置对应图标
        if (list.get(groupPosition).getPlatName().equals("FC")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.fc);
        }
        if (list.get(groupPosition).getPlatName().equals("bit-z")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.bit);
        }
        if (list.get(groupPosition).getPlatName().equals("bibox")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.bibox);
        }
        if (list.get(groupPosition).getPlatName().equals("币蛋")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.bidan);
        }
        if (list.get(groupPosition).getPlatName().equals("比特儿")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.bter);
        }
        if (list.get(groupPosition).getPlatName().equals("中币")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.zb);
        }
        if (list.get(groupPosition).getPlatName().equals("OKEX")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.okex);
        }
        if (list.get(groupPosition).getPlatName().equals("币安网")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.bian);
        }
        if (list.get(groupPosition).getPlatName().equals("火币网")){

            groupViewHolder.se_ptIcon.setImageResource(R.mipmap.huobi);
        }
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
     * android.view.ViewGroup)
     */

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.res_jyd = convertView.findViewById(R.id.res_jyd);
            childViewHolder.res_icon = convertView.findViewById(R.id.res_icon);
            childViewHolder.blank = convertView.findViewById(R.id.blank);
            convertView.setTag(childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.res_jyd.setText(list.get(groupPosition).getList().get(childPosition).getCoinName() + "/"
                + list.get(groupPosition).getList().get(childPosition).getRelativeTrade());

        if (isLastChild){

            childViewHolder.blank.setVisibility(View.VISIBLE);
        }else {

            childViewHolder.blank.setVisibility(View.GONE);
        }

        //设置图标
        if (list.get(groupPosition).getList().get(childPosition).getFocusFlag().equals("1")) {
            childViewHolder.res_icon.setImageResource(R.mipmap.tianjia_dianjihou_icon);
        } else {

            childViewHolder.res_icon.setImageResource(R.mipmap.tianjia_icon);
        }

        //子级图标点击事件
        childViewHolder.res_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PreferencesUtil.getInstance().putInt(context, Constant.GROUPPOSITION, groupPosition);
                PreferencesUtil.getInstance().putInt(context, Constant.CHILDPOSITION, childPosition);

                if (list.get(groupPosition).getList().get(childPosition).getFocusFlag() != null &&
                        list.get(groupPosition).getList().get(childPosition).getFocusFlag().equals("1")){

//                    long rowId = list.get(groupPosition).getList().get(childPosition).getRowId();
//                    String coinId = list.get(groupPosition).getList().get(childPosition).getCoinId();
//                    String platformId = list.get(groupPosition).getList().get(childPosition).getPlatformId();
//                    String relativeTrade = list.get(groupPosition).getList().get(childPosition).getRelativeTrade();
//                    token = PreferencesUtil.getInstance().getString(context, "token");
//                    Map<String, String> map = new HashMap<>();
//                    map.put("token", token);
//                    map.put("coinId", coinId);
//                    map.put("rowId", rowId+"");
//                    map.put("platformId", platformId);
//                    map.put("relativeTrade", relativeTrade);
//                    LKUtil.getHttpManager().post(HttpConstant.DELETE_URL, map, delHandler);

                    dialogShow(groupPosition, childPosition);

                }else {

                    String coinId = list.get(groupPosition).getList().get(childPosition).getCoinId();
                    String platformId = list.get(groupPosition).getList().get(childPosition).getPlatformId();
                    String relativeTrade = list.get(groupPosition).getList().get(childPosition).getRelativeTrade();
                    String coinName = list.get(groupPosition).getList().get(childPosition).getCoinName();
                    String platformName = list.get(groupPosition).getList().get(childPosition).getPlatformName();
                    token = PreferencesUtil.getInstance().getString(context, Constant.TOKEN);
                    Map<String, Object> map = new HashMap<>();
                    map.put("token", token);
                    map.put("coinId", coinId);
                    map.put("coinName", coinName);
                    map.put("platformName", platformName);
                    map.put("platformId", platformId);
                    map.put("relativeTrade", relativeTrade);
                    LKUtil.getHttpManager().postBody(HttpConstant.ADDBI_URL, map, addHandler);
                }

            }
        });

        return convertView;
    }

    /**
     * 添加币的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> addHandler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);

            int i = PreferencesUtil.getInstance().getInt( context,Constant.GROUPPOSITION);
            int i1 = PreferencesUtil.getInstance().getInt(context, Constant.CHILDPOSITION);
            list.get(i).getList().get(i1).setFocusFlag("1");

            notifyDataSetChanged();
            dialogHint.setContent("自选成功");
//            coinAdapter.updateItemById(rowId,"1");
            dialogHint.show();

        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 移除币的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> delHandler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);

            int i = PreferencesUtil.getInstance().getInt( context,Constant.GROUPPOSITION);
            int i1 = PreferencesUtil.getInstance().getInt(context, Constant.CHILDPOSITION);
            list.get(i).getList().get(i1).setFocusFlag("0");
            notifyDataSetChanged();
//            coinAdapter.updateItemById(rowId,"0");
           Toast.makeText(context, "移除成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setList(List<SearchBean> list) {
        this.list = list;
    }

    @Override
    public void onSure() {
        int i = PreferencesUtil.getInstance().getInt( context,Constant.GROUPPOSITION);
        int i1 = PreferencesUtil.getInstance().getInt(context, Constant.CHILDPOSITION);

        List<ParamUtils.NameValue> parList = ParamUtils.build().put("platformName", list.get(i).getList().get(i1).getPlatformName())
                .put("coinName", list.get(i).getList().get(i1).getCoinName()).put("coinId", list.get(i).getList().get(i1).getCoinId())
                .put("platformId", list.get(i).getList().get(i1).getPlatformId()).put("relativeTrade", list.get(i).getList().get(i1).getRelativeTrade())
                .create();
        IntentUtils.startAty(context, SetWarnActivity.class,parList);
        ((Activity)context).finish();
    }

    @Override
    public void onCancel() {

    }

    public Context getContext() {
        return context;
    }

    static class GroupViewHolder {
        TextView se_pt;
        ImageView se_ptIcon;
    }

    static class ChildViewHolder {
        TextView res_jyd;
        ImageView res_icon;
        View blank;
    }

    /**
     * 移除自选币弹框
     * @param groupPosition
     * @param childPosition
     */
    private void dialogShow(final int groupPosition, final int childPosition) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View v = inflater.inflate(R.layout.dialog_del, null);

        Button btn_sure = (Button) v.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) v.findViewById(R.id.dialog_canle);

        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        //确认移除点击事件
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                long rowId = list.get(groupPosition).getList().get(childPosition).getRowId();
                String coinId = list.get(groupPosition).getList().get(childPosition).getCoinId();
                String platformId = list.get(groupPosition).getList().get(childPosition).getPlatformId();
                String relativeTrade = list.get(groupPosition).getList().get(childPosition).getRelativeTrade();
                token = PreferencesUtil.getInstance().getString(context, "token");
                Map<String, Object> map = new HashMap<>();
                map.put("token", token);
                map.put("coinId", coinId);
                map.put("rowId", rowId+"");
                map.put("platformId", platformId);
                map.put("relativeTrade", relativeTrade);
                LKUtil.getHttpManager().postBody(HttpConstant.DELETE_URL, map, delHandler);
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }

}
